package com.xoa.controller.rms.lend;

import com.xoa.model.rms.*;
import com.xoa.model.users.Users;
import com.xoa.service.rms.RmsFileService;
import com.xoa.service.rms.RmsLendService;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import net.sf.ehcache.util.SetAsList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/RmsLendController")
public class RmsLendController {

    @Autowired
    private RmsFileService rmsFileService;

    /**
     * 作者: 郭心雨
     * 日期: 2018/8/13
     * 说明: 案卷借阅首页
     */
    @RequestMapping("/paperManage")
    public String paperManage(){
        return "app/rms/paperBorrowing/index";
    }

    /**
     * 作者: 郭心雨
     * 日期: 2018/8/13
     * 说明: 借阅记录
     */
    @RequestMapping("/loaningRecord")
    public String loaningRecord(){
        return "app/rms/paperBorrowing/loaningRecord";
    }

    /**
     * 作者: 郭心雨
     * 日期: 2018/8/13
     * 说明: 借阅申请
     */
    @RequestMapping("/loanApplication")
    public String loanApplication(){
        return "app/rms/paperBorrowing/loanApplication";
    }

    /**
     * 作者: 郭心雨
     * 日期: 2018/8/13
     * 说明: 借阅查询
     */
    @RequestMapping("/loaningQuery")
    public String loaningQuery(){
        return "app/rms/paperBorrowing/loaningQuery";
    }

    /**
     * 作者: 郭心雨
     * 日期: 2018/8/13
     * 说明: 借阅审批
     */
    @RequestMapping("/reviewApproval")
    public String reviewApproval(){
        return "app/rms/paperBorrowing/reviewApproval";
    }


    /**
     * 查看借阅详情
     * @return
     */
    @RequestMapping("/borrowingTheQuery")
    public String borrowingTheQuery(){
        return "app/rms/paperBorrowing/borrowingTheQuery" ;
    }
//app/rms/paperBorrowing/examineAndApprove.jsp

    /**
     * 借阅审批
     * @return
     */
    @RequestMapping("/examineAndApprove")
    public String examineAndApprove(){
        return "app/rms/paperBorrowing/examineAndApprove";
    }
    /**
     * 作者: 郭心雨
     * 日期: 2018/8/13
     * 说明: 借阅-文件详情
     */
    @RequestMapping("/loanDetail")
    public String loanDetail(){
        return "app/rms/paperBorrowing/loanDetail";
    }

    /**
     * 作者: 郭心雨
     * 日期: 2018/8/13
     * 说明: 借阅-查看文件
     */
    @RequestMapping("/seeFile")
    public String seeFile(){
        return "app/rms/paperBorrowing/seeFile";
    }

    /**
     * 作者: 郭心雨
     * 日期: 2018/8/13
     * 说明: 借阅统计
     */
    @RequestMapping("/borrowingStatistics")
    public String borrowingStatistics(){
        return "app/rms/paperBorrowing/borrowingStatistics";
    }

    //我的借阅
    @RequestMapping("/iBorrow")
    public String iBorrow(){
        return "app/rms/paperBorrowing/iBorrow" ;
    }

    //借阅批准
    @RequestMapping("/ratify")
    public String ratify(){
        return "app/rms/paperBorrowing/ratify" ;
    }

    //借阅不批准
    @RequestMapping("/nonPlacet")
    public String nonPlacet(){
        return "app/rms/paperBorrowing/nonPlacet";
    }

    //借阅审批详情
    @RequestMapping("/approvalForDetails")
    public String approvalForDetails(){
        return "app/rms/paperBorrowing/approvalForDetails";
    }

    //借阅申请页面的借阅
    @RequestMapping("/lendingOperations")
    public String lendingOperations(){
        return "app/rms/paperBorrowing/lendingOperations";
    }


    //文件借阅申请-借阅app/rms/paperBorrowing/documentLibrary.jsp

    @RequestMapping("/documentLibrary")
    public String documentLibrary(){
        return "app/rms/paperBorrowing/documentLibrary";
    }
    /**
     * 作者: 郭心雨
     * 日期: 2018/8/13
     * 说明: 借阅统计
     */
    @RequestMapping("/fileStatistics")
    public String fileStatistics(){
        return "app/rms/paperBorrowing/fileStatistics";
    }
    @Autowired
    private RmsLendService rmsLendService;

    /**
     * 郭心雨
     * 2018.07.31
     * 条件查询列表
     * 2018.07.31
     * @param allow  允许状态
     * @return
     */
    @RequestMapping("/selRmsLend")
    @ResponseBody
    public ToJson selRmsLend(HttpServletRequest request,String allow,@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                             @RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                             @RequestParam(name = "useFlag", required = false, defaultValue = "true")  Boolean useFlag){
        //获取当前登录用户作为审批人
        Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        String userId=users.getUserId();
        return rmsLendService.selRmsLend( userId, allow, page, pageSize, useFlag);
    }

    /**
     * 郭心雨
     * 2018.07.31
     * 更改文件借阅状态（归还，批准，不批）
     * @param deleteFlag 删除状态
     * @param allow 允许状态
     * @param lendId 文件id
     * @param returnTime 归还时间
     * @param allowTime 审批时间
     * @return
     */
    @RequestMapping("/modifyRmsLendById")
    @ResponseBody
    public ToJson modifyRmsLendById(HttpServletRequest request,String operator,String deleteFlag,String allow,Integer lendId,String returnTime,String allowTime){
        //获取当前登录用户作为审批人
        Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        operator=users.getUserId();
        return rmsLendService.modifyRmsLendById(operator,deleteFlag,allow,lendId,returnTime,allowTime);
    }

    /**
     * 郭心雨
     * 2018.08.01
     * 点击借阅,增加一条借阅信息,从文件管理里借阅
     * @param rmsLend
     * @return
     */
    @RequestMapping("/addRmsLend")
    @ResponseBody
    public ToJson addRmsLend(HttpServletRequest request,RmsLend rmsLend , Integer fileId){
        //获取当前登录用户作为借阅人
        Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        //借阅人是当前登录用户
        rmsLend.setApprove(users.getUserName());
        rmsLend.setUserId(users.getUserId());
        //点击借阅后默认为待批准借阅
        rmsLend.setAllow("0");
        //删除状态为0 未删除
        rmsLend.setDeleteFlag("0");
        //创建时默认审核人为null
        rmsLend.setOperator("");
        //数据来源文件管理
        rmsLend.setType("0");
        //关联案卷id
        rmsLend.setFileId(fileId);

        //申请时间
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd HH:MM:ss");
        rmsLend.setAddTime(sf.format(new Date()));

//        rmsLend.setOperator(users.getUserId());
        return rmsLendService.addRmsLend(rmsLend);
    }

    /**
     * 批量添加
     * 郭心雨
     * 2018.08.14
     * @param request
     * @param fileIds
     * @return
     */
    @RequestMapping("/insertLensd")
    @ResponseBody
    public ToJson insertLensd(HttpServletRequest request,Integer[] fileIds){
        return rmsLendService.insertLensd( request,fileIds);
    }

    /**
     * 郭心雨
     * 模糊动态查询
     * 2018-08-14
     * @param page
     * @param pageSize
     * @param useFlag
     * @param rmsFile
     * @param roomName
     * @param rollName
     * @return
     */
    @RequestMapping("/selAllLendBy")
    @ResponseBody
    public ToJson selAllLendBy(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                               @RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                               @RequestParam(name = "useFlag", required = false, defaultValue = "true")  Boolean useFlag,RmsFile rmsFile,String roomName,String rollName){
        return rmsLendService.selAllLendBy(  page, pageSize, useFlag,rmsFile, roomName, rollName);
    }

    /**
     * 案卷统计，查看案卷并查出案卷中有多少个文件
     * 郭心雨
     * 2018.08.16
     * @param rmsRollWithBLOBs
     * @param pageParams
     * @return
     */
    @ResponseBody
    @RequestMapping("/rollCount")
    public ToJson<RmsRollWithBLOBs> rollCount(RmsRollWithBLOBs rmsRollWithBLOBs, PageParams pageParams) {
        return rmsLendService.rollCount(rmsRollWithBLOBs, pageParams);
    }

    /**
     * 借阅统计
     * 郭心雨
     * 2018.08.16
     * @param rmsFileWithBLOBs
     * @param pageParams
     * @return
     */
    @ResponseBody
    @RequestMapping("/fileJieYueCount")
    public ToJson<RmsFileWithBLOBs> fileJieYueCount(RmsFileWithBLOBs rmsFileWithBLOBs, PageParams pageParams) {
        return rmsLendService.fileJieYueCount(rmsFileWithBLOBs, pageParams);
    }
    @ResponseBody
    @RequestMapping("/selRmsLendByJieYe")
    public ToJson selRmsLendByJieYe(Integer fileId,HttpServletRequest request,String allow,@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                             @RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize,
                             @RequestParam(name = "useFlag", required = false, defaultValue = "true")  Boolean useFlag){
        //获取当前登录用户作为审批人
        Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        String userId=users.getUserId();
        return rmsLendService.selRmsLendByJieYe( fileId, allow, page, pageSize, useFlag);
    }

    /**
     *根据传入的状态id 修改审核状态
     */
    @RequestMapping("/updateAllow")
    @ResponseBody
    public ToJson updateAllow(RmsLend rmsLend ){
        ToJson json =new ToJson<>(1,"err");
        try {
            Integer data = rmsLendService.updateAllow(rmsLend);
            if(data !=null ){
                json.setObj1(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }


    //根据审批状态查询
    @RequestMapping("/selectAllow")
    @ResponseBody
    public ToJson selectAllow(String allow){
        ToJson json = new ToJson<>(1,"err");
        try {
            List<RmsLend> data = rmsLendService.selectAllow(allow);
            if(data !=null ){
                json.setObj(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }


    //删除 借阅信息
    @RequestMapping("/deleteFlag")
    @ResponseBody
    public ToJson deleteFlag(Integer lendId){
        ToJson toJson = new ToJson<>(1,"err");
        try {
            Integer data = rmsLendService.deleteFlag(lendId);
            if (data != null){
                toJson.setObj1(data);
                toJson.setFlag(0);
                toJson.setMsg("true");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return toJson;
    }

    //查看借阅详情
    @RequestMapping("/selectByLend")
    @ResponseBody
    public ToJson selectByLend(Integer lendId){
        ToJson toJson = new ToJson<>(1,"err");
        try {
            RmsLend data = rmsLendService.selectByLend(lendId);
            if (data != null){
                toJson.setObj1(data);
                toJson.setFlag(0);
                toJson.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toJson;
    }

    //查询所有借阅记录
    @RequestMapping("/selectByCount")
    @ResponseBody
    public ToJson selectByCount( Integer page, Integer pageSize, Boolean useFlag ,RmsLend rmsLend){
        ToJson toJson = new ToJson<>(1,"err");
        Map<String,Object> map= new HashMap<String, Object>();
        try {
            List<RmsLend> data = rmsLendService.selectByCount(page, pageSize, useFlag, map,rmsLend);

            if (data != null){
                PageParams pageParams = (PageParams)map.get("page");
                toJson.setTotleNum(pageParams.getTotal());
                toJson.setObj(data);
                toJson.setFlag(0);
                toJson.setMsg("true");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return toJson;
    }

    /**
     * 添加 从案卷管理里面借阅
     * @param request
     * @param rmsLend
     * @return
     */
    @RequestMapping("/addRmsLendRoll")
    @ResponseBody
    public ToJson addRmsLendRoll(HttpServletRequest request,RmsLend rmsLend , Integer rollId){
        //获取当前登录用户作为借阅人
        Users users= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
        //借阅人是当前登录用户
        rmsLend.setApprove(users.getUserName());
        rmsLend.setUserId(users.getUserId());
        //点击借阅后默认为待批准借阅
        rmsLend.setAllow("0");
        //删除状态为0 未删除
        rmsLend.setDeleteFlag("0");
        //创建时默认审核人为null
        rmsLend.setOperator("");
        //数据来源案卷管理
        rmsLend.setType("1");
        //关联案卷管理id
        rmsLend.setFileId(rollId);

        //申请时间
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-mm-dd HH:MM:ss");
        rmsLend.setAddTime(sf.format(new Date()));

//        rmsLend.setOperator(users.getUserId());
        return rmsLendService.addRmsLend(rmsLend);
    }

    //模糊查询
    @RequestMapping("/queryByCount")
    @ResponseBody
    public ToJson queryByCount(Integer page, Integer pageSize, Boolean useFlag , RmsRoll rmsRoll, RmsFile rmsFile,RmsLend rmsLend){
        ToJson toJson = new ToJson<>(1,"err");
        Map<String,Object> map= new HashMap<String, Object>();
        try {
            List<RmsLend> data = rmsLendService.queryByCount(page, pageSize, useFlag, map, rmsFile, rmsRoll,rmsLend);
            if (data != null){
                List<RmsLend>  rmsLends = (List<RmsLend>) map.get("list");
                toJson.setTotleNum(rmsLends.size());
                toJson.setObj(data);
                toJson.setFlag(0);
                toJson.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toJson;
    }

    //我的借阅
    @RequestMapping("/selectLend")
    @ResponseBody
    public ToJson selectLend( Integer page, Integer pageSize, Boolean useFlag ,RmsLend rmsLend){
        ToJson toJson = new ToJson<>(1,"err");
        Map<String,Object> map = new HashMap();
        try {
            List<RmsLend> data = rmsLendService.select(page,pageSize,useFlag,map, rmsLend);
            if (data != null){
                PageParams pageParams  = (PageParams)map.get("page");
                toJson.setTotleNum(pageParams.getTotal());
                toJson.setObj(data);
                toJson.setFlag(0);
                toJson.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  toJson;
    }

    //文件管理
    @RequestMapping("/selectAndFile")
    @ResponseBody
    public ToJson selectAndFile(RmsFileWithBLOBs rmsFileWithBLOBs, PageParams pageParams){

        return rmsFileService.query(rmsFileWithBLOBs,pageParams);
    }

}
